﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Space
{
    public class BombGun : Gun
    {
        public BombGun(Ship ship) : base(ship) { }

        public override void Fire()
        {
            Planet targetPlanet = null;

            Bomb b = new Bomb(Ship)
            {
                Position = Position,
                Damage = 1,
                Speed = 0.1f,
                SplashRadius = 2f,
                Ship = Ship
            };

            IData data = Game.Services.GetService<IData>();

            foreach (SolarSystem s in data.SolarSystems)
            {
                foreach (Planet p in s.Planets.Where(p => p.Player != Ship.Player))
                {
                    if ((p.Position - Position).Length() <= Range)
                    {
                        targetPlanet = p;
                        goto outer;
                    }
                }
            }
        outer:
            if (targetPlanet != null)
            {
                b.TargetPosition = targetPlanet.Position;
            }
            else
            {
                b.TargetPosition = Position + 10 * new Vector3((float)Math.Cos(Ship.Angle), 0, (float)Math.Sin(Ship.Angle));
            }
            data.AddWeapon(b);

            base.Fire();
        }
    }
}
